const mongoose = require('mongoose');
//连接的数据库不需要先创建
mongoose.connect('mongodb://localhost/bookshop', {
    useUnifiedTopology: true,
    useNewUrlParser: true
});

//创建集合(表)规则，不是集合
const schema = new mongoose.Schema({
    name: String,
    price: Number,
    count: Number,
    createDate: {
        type: Date,
        default: Date.now,
    }
});

//创建集合(表)
const model = new mongoose.model("books", schema);

//保存集合结构到数据库中
// model.save();

//无条件查询所有数据
// model.find((err, result) => {
//     console.log(result);
// });

//查询spring书籍
// model.find({name: /spring/i, price: {$gte: 40}}, (err, result) => {
//     console.log(result);
// });

//查询一条数据
// model.findOne({name: /spring/i, price: {$gte: 40}}, (err, result) => {
//     console.log(result);
// })

//更新多条数据
// model.updateMany({name: /spring/i, price: {$gte: 40}}, {count: 100}, (err, result) => {
//     console.log(result);
// });

//删除一条数据
// model.deleteOne({_id: "60e46a4d2ec83324b88279bc"}, (err, result) => {
//     console.log(result);
// })
//添加一条记录
// model.create({
//     name: "张三",
//     price: 100,
//     count: 100
// }, (err, result) => {
//     console.log(result);
// });
//添加多条数据
// model.insertMany([{
//     name: "李四",
//     price: 100,
//     count: 100
// }, {
//     name: "五环",
//     price: 100,
//     count: 100
// }], (err, result) => {
//     console.log(result);
// })

//分页查询
//limit：pageSize，skip：pageSize * (pageNum - 1)
model.find({name: /spring/i}).limit(2).skip(10).exec((err, result) => {
    console.log(result);
});